package com.zja.www.App;

import com.zja.www.controller.AdminController;
import com.zja.www.controller.DoctorController;
import com.zja.www.controller.UserController;
import com.zja.www.dao.ScheduleDao;
import com.zja.www.dao.impl.*;

import com.zja.www.entity.Department;
import com.zja.www.entity.Doctor;
import com.zja.www.entity.Schedule;

import com.zja.www.service.impl.*;
import com.zja.www.view.DepartmentView;
import com.zja.www.view.DoctorView;
import com.zja.www.view.UserView;
import com.zja.www.view.AdminView;


import java.util.InputMismatchException;
import java.util.Scanner;

public class App {
    public static void main(String[] args) {
        UserDaoImpl userDao = new UserDaoImpl();
        DoctorDaoImpl doctorDao = new DoctorDaoImpl();
        MedicalRecordDaoImpl medicalRecordDao = new MedicalRecordDaoImpl();
        ScheduleDao scheduleDao = new ScheduleDaoImpl();
        AdminDaoImpl adminDao = new AdminDaoImpl();
        Schedule schedule = new Schedule();
        DepartmentDaoImpl departmentDao = new DepartmentDaoImpl();


        // 2. 初始化服务层
        UserServiceImpl userService = new UserServiceImpl();
        DoctorServiceImpl doctorService = new DoctorServiceImpl(doctorDao);
        MedicalRecordServiceImpl medicalRecordService = new MedicalRecordServiceImpl(medicalRecordDao);
        AdminServiceImpl adminService = new AdminServiceImpl(adminDao,userDao,doctorDao,scheduleDao);
        ScheduleServiceImpl scheduleService = new ScheduleServiceImpl(scheduleDao);
        DepartmentServiceImpl departmentService = new DepartmentServiceImpl(departmentDao);

        // 3. 初始化控制器
        UserController userController = new UserController(
                userService,
                doctorService,
                medicalRecordService
        );

        AdminController adminController = new AdminController(
                adminService,
                userService,
                doctorService,
                medicalRecordService
        );

        DoctorController doctorController = new DoctorController(
                userService,
                doctorService,
                medicalRecordService
        );

        // 4. 初始化视图
        DepartmentView departmentView = new DepartmentView(
                userService,
                doctorService,
                medicalRecordService,
                departmentService

        );

        UserView userView = new UserView(
                userController,
                userService,
                doctorService,
                medicalRecordService,
                scheduleService,
                departmentService
        );

        //互相注入
        departmentView.setUserView(userView);
        userView.setDepartmentView(departmentView);

        AdminView adminView = new AdminView(
                adminController,
                adminService,
                userService,
                doctorService,
                medicalRecordService,
                departmentService
        );

        Doctor defaultDoctor = new Doctor();
        defaultDoctor.setId(0);

        DoctorView doctorView = new DoctorView(
                userService,
                doctorService,
                medicalRecordService,
                scheduleService,
                doctorController,
                defaultDoctor
        );




        // 启动系统，显示主菜单
        showMainMenu(userView, adminView, doctorView);
    }

    /**
     * 显示系统主菜单
     *
     * @param userView   用户视图
     * @param adminView  管理员视图
     * @param doctorView 医生视图
     */
    private static void showMainMenu(UserView userView, AdminView adminView, DoctorView doctorView) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("\n=============欢迎进入校医院预约挂号管理系统===============");
            System.out.println("=======================主菜单==========================");
            System.out.println("======================请选择角色=========================");
            System.out.println("1.学生");
            System.out.println("2.医生");
            System.out.println("3.管理员");
            System.out.println("0.退出系统");
            System.out.print("请选择：");

            try {
                int choice = scanner.nextInt();
                scanner.nextLine();

                switch (choice) {
                    case 1:
                        userView.showStudentMenu();
                        break;
                    case 2:
                        doctorView.showDoctorMenu(doctorView);
                        break;
                    case 3:
                        adminView.showAdminMenu(adminView);
                        break;
                    case 0:
                        System.out.println("感谢使用，再见！");
                        scanner.close();
                        System.exit(0);  // 退出系统
                    default:
                        System.out.println("输入无效，请重新选择！");
                }
            } catch (InputMismatchException e) {
                System.out.println("请输入有效数字！");
                scanner.nextLine();
            }
        }
    }


}

